package com.yyk.yiliao.adapter.anzhengzhaoyao;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.yyk.yiliao.R;
import com.yyk.yiliao.bean.StoreCustomclass_Info;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ${zk} on 2018/8/22 0022.
 * https://blog.csdn.net/u012570402/article/details/50536931    用ListView和GridView实现的，第一个item默认选中的二级列表
 */

public class MyListViewAdapter extends BaseAdapter {
    private Context context;
    private List<StoreCustomclass_Info.DataBean> list;
    private int selected = 0;

    public void setSelected(int position) {
        this.selected = position;
    }

    /**
     * 根据selected（被点击的item）的值动态的决定控件的属性
     *
     * @param position
     * @param view
     */
    public void disposalView(int position, View view) {           //此方法是在基类AbsBaseAdapter定义的一个方法，复写可以控制item的view

        if (position == selected) {
            view.setBackgroundResource(R.color.white);
            ViewHolder viewHolder = (ViewHolder) view.getTag();
            TextView textView = (TextView) viewHolder.getView(R.id.textView);
            textView.setTextColor(context.getResources().getColor(R.color.blue_14));
        } else {
            view.setBackgroundResource(R.color.white_f6f);
            ViewHolder viewHolder = (ViewHolder) view.getTag();
            TextView textView = (TextView) viewHolder.getView(R.id.textView);
            textView.setTextColor(context.getResources().getColor(R.color.black));
        }
    }

    public MyListViewAdapter(Context context, List<StoreCustomclass_Info.DataBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.adapter_azzy_listview_tv, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        TextView textView = (TextView) holder.getView(R.id.textView);
        textView.setText(list.get(position).getName());
        disposalView(position, convertView);
        return convertView;
    }

//    class ViewHolder {
//        TextView textView;
//
//        public ViewHolder(View view) {
//            textView = view.findViewById(R.id.textView);
//        }
//    }

    /**
     * ViewHolder的作用，缓存item布局中的控件对象，以免下一次再fingViewById
     */
    class ViewHolder {
        //使用一个Map集合，用来存放需要使用到的控件对象，key值即为控件ID
        private Map<Integer, View> mapCache = new HashMap<>();
        //item的布局对象
        private View layoutView;

        public ViewHolder(View layoutView) {
            this.layoutView = layoutView;
        }

        //提供一个获得布局中子控件的方法，参数为控件id
        public View getView(int id) {
            if (mapCache.containsKey(id)) {//查看该控件是否存在于map集合中
                return mapCache.get(id);//如果存在直接从map中获得该控件，并返回
            } else {
                View view = layoutView.findViewById(id);//如果不存在，那么需要从布局对象中，findViewById
                mapCache.put(id, view);//缓存进map集合中
                return view;
            }
        }
    }
}
